package vng.survey.server;

import com.vng.zing.lib.Config;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadPool;
import vng.survey.controller.AjaxUpdateSurvey;
import vng.survey.controller.AjaxAddSurvey;
import vng.survey.controller.AjaxDeleteResponse;
import vng.survey.controller.AjaxDeleteSurvey;
import vng.survey.controller.AjaxHome;
import vng.survey.controller.AjaxResponseDetail;
import vng.survey.controller.AjaxStatisticController;
import vng.survey.controller.EditSurveyController;
import vng.survey.controller.HomeController;
import vng.survey.controller.NewSurveyController;
import vng.survey.controller.ResponseController;
import vng.survey.controller.StatisticController;
import vng.survey.controller.ResponseDetailController;
import vng.survey.controller.AjaxSubmitResponse;

import vng.survey.controller.test.AddSurveyControllerTest;
import vng.survey.filter.RoutingFilter;

public class WebServer {

    public void start() throws Exception {
        //Server server = new Server();


        //init notifybus
        //CommonModel.init();

        ThreadPool theadPool = new QueuedThreadPool(100, 2000);

        Server server = new Server(theadPool);


        HttpConfiguration http_config = new HttpConfiguration();
        http_config.setSecureScheme("https");
        http_config.setSecurePort(8443);
        http_config.setOutputBufferSize(32768);
        ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
        http.setPort(Config.getParamInt("jetty_server", "port"));
        http.setIdleTimeout(30000);
        server.setConnectors(new Connector[]{http});
        
        
        ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        
        //filters
        handler.addFilter(RoutingFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
        
        
        //ServletHandler servletHandler = new ServletHandler();
//        servletHandler.addServletWithMapping(NewSurveyController.class, "/new");
//        servletHandler.addServletWithMapping(HomeController.class, "/home");
//        servletHandler.addServletWithMapping(ResponseController.class, "/response");
//        servletHandler.addServletWithMapping(StatisticController.class, "/stat");
//        servletHandler.addServletWithMapping(EditSurveyController.class, "/edit");
//        servletHandler.addServletWithMapping(AjaxAddSurvey.class, "/add");
//        servletHandler.addServletWithMapping(AjaxSubmitResponse.class, "/submit");
//        servletHandler.addServletWithMapping(AddSurveyControllerTest.class, "/add/test");
//        servletHandler.addServletWithMapping(AjaxUpdateSurvey.class, "/edited");
//        servletHandler.addServletWithMapping(AjaxStatisticController.class, "/answer");
//        servletHandler.addServletWithMapping(ResponseDetailController.class, "/detail");
//        servletHandler.addServletWithMapping(AjaxResponseDetail.class, "/responsedetail");
//        servletHandler.addServletWithMapping(AjaxHome.class, "/paging");
//        servletHandler.addServletWithMapping(AjaxDeleteResponse.class, "/delres");
//        servletHandler.addServletWithMapping(TestController.class, "/test");
//        servletHandler.addServletWithMapping(FilterController.class, "/survey/*");
        
        //handler.addFilter("vng.survey.controller.NewClass", "/survey/*", EnumSet.of(DispatcherType.REQUEST));
        //     servletHandler.addServletWithMapping(AjaxAddSurvey.class, "/add");
//        servletHandler.addServletWithMapping(AjaxSubmitResponse.class, "/submit");
       handler.addServlet(AddSurveyControllerTest.class, "/add/test");
//        servletHandler.addServletWithMapping(AjaxUpdateSurvey.class, "/edited");
//        servletHandler.addServletWithMapping(AjaxStatisticController.class, "/answer");
//        servletHandler.addServletWithMapping(ResponseDetailController.class, "/detail");
//        servletHandler.addServletWithMapping(AjaxResponseDetail.class, "/responsedetail");
//        servletHandler.addServletWithMapping(AjaxHome.class, "/paging");
//        servletHandler.addServletWithMapping(AjaxDeleteResponse.class, "/delres");

//<<<<<<< .mine
//        handler.addServlet("vng.survey.controller.RoutingController", "/survey/*");
//        handler.addServlet("vng.survey.controller.HomeController", "/home");
//        handler.addServlet("vng.survey.controller.NewSurveyController", "/new");
//        handler.addServlet("vng.survey.controller.ResponseController", "/response");
//         handler.addServlet("vng.survey.controller.StatisticController", "/stat");
//         handler.addServlet("vng.survey.controller.EditSurveyController", "/edit");
//          handler.addServlet("vng.survey.controller.ResponseDetailController", "/detail");

        handler.addServlet("vng.survey.controller.RoutingController", "/*");
          handler.addServlet(AjaxAddSurvey.class, "/add");
        handler.addServlet(AjaxSubmitResponse.class, "/submit");
          handler.addServlet(AddSurveyControllerTest.class, "/add/test");
         handler.addServlet(AjaxUpdateSurvey.class, "/edited");
         handler.addServlet(AjaxStatisticController.class, "/answer");
          handler.addServlet(AjaxResponseDetail.class, "/responsedetail");
         handler.addServlet(AjaxHome.class, "/paging");
         handler.addServlet(AjaxDeleteResponse.class, "/delres");
          handler.addServlet(AjaxDeleteSurvey.class, "/delsur");
//         
        handler.addServlet("vng.survey.controller.HomeController", "/home/*");
        handler.addServlet("vng.survey.controller.NewSurveyController", "/new/*");
        handler.addServlet("vng.survey.controller.ResponseController", "/response/*");
         handler.addServlet("vng.survey.controller.StatisticController", "/stat/*");
         handler.addServlet("vng.survey.controller.EditSurveyController", "/edit/*");
          handler.addServlet("vng.survey.controller.ResponseDetailController", "/detail/*");
           
        ResourceHandler resource_handler = new ResourceHandler();
//		resource_handler.setWelcomeFiles(new String[]{ "index.html" });
        resource_handler.setResourceBase(Config.getParamStr("resource", "resource"));
        ContextHandler context0 = new ContextHandler();
        context0.setContextPath("/");
        context0.setHandler(resource_handler);

        HandlerList handlers = new HandlerList();
        // handlers.setHandlers(new Handler[] { handler });
        handlers.setHandlers(new Handler[]{context0, handler});
        server.setHandler(handlers);

//		MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement(System.getProperty("java.io.tmpdir"));
//		server.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);

        server.setStopAtShutdown(true);
//		server.setGracefulShutdown(1000);
//		server.setSendServerVersion(true);

        server.start();
        server.join();
    }
}
